<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/29
 * Time: 13:45
 */
namespace app\admin\controller;
use app\common\Tree;
class AdminRole extends Base{
    public function index(){
        $model=db("admin_role");
        $list=$model->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function add(){
        $model=db('admin_role');
        if($_POST){
            $role_name=I("post.role_name",'','trim');
            $remark=I("post.remark",'','trim');
            $status=I("post.status",1,'');
            $role_name || die(json_error('权限组名称不能为空'));
            $data=[
                'role_name'=>$role_name,
                'remark'=>$remark,
                'status'=>$status,
                'add_time'=>time(),
            ];
            $res=$model->insert($data);
            $res || die(json_error('添加失败'));
            die(json_success('添加成功'));
        }
        return $this->fetch();
    }

    public function edit(){
        $model=db('admin_role');
        if($_POST){
            $role_name=I("post.role_name",'','trim');
            $remark=I("post.remark",'','trim');
            $status=I("post.status",1,'');
            $id=I("post.id",0,'intval');
            $id >0 || die(json_error('参数错误'));
            $role_name || die(json_error('权限组名称不能为空'));
            $data=[
                'role_name'=>$role_name,
                'remark'=>$remark,
                'status'=>$status,
                'update_time'=>time(),
            ];
            $res=$model->where(['id'=>$id])->update($data);
            $res || die(json_error('编辑失败'));
            die(json_success('编辑成功'));
        }
        $id=I("param.id",0,'intval');
        $info=$model->where(['id'=>$id])->find();
        $info || $this->error('参数错误');

        $this->assign('info',$info);

       return  $this->fetch();
    }

    function tre($arr,$adds=''){
        $icon= array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
        $str  = "<tr id='node-\$id' \$parentid_node>" .
            "<td style='padding-left:10px;'>\$spacer \$checked \$name</td>
	                    </tr>";
        $number=1;
        $str_group='';
        $sid='';
        $parentid=0;
        static $s='';
            foreach($arr as $id=>$value){
                $j=$k='';
                if($number==count($arr)){
                    $j .= $icon[2];
                }else{
                    $j .= $icon[1];
                    $k = $adds ? $icon[0] : '';
                }
                $spacer = $adds ? $adds.$j : '';
                $selected = $id==$sid ? 'selected' : '';
                @extract($value);
                $parentid == 0 && $str_group ? eval("\$nstr = \"$str_group\";") : eval("\$nstr = \"$str\";");
                $s .= $nstr;
                $nbsp ="&nbsp";

                if(isset($value['list']) && is_array($value['list']) && $value['list']){
                    $this->tre($value['list'],$adds.$k.$nbsp);
                }
                $number++;
            }
            return $s;
    }

    public function test(){
        $ar=db('menu')->select();
        $a=[];
        foreach($ar as $k=>$v){
            if($v['pid']>0){
                $v['checked']="<input type='checkbox' name='menu_id[]' value='".$v['id']."' class='J_checkitem'   >";
            }else{
                $v['checked']='';
            }

            $v['parentid_node']="888";
            $a[$v['id']]=$v;
        }
        $list=[];


        $str='';
        foreach($a as $k=>$v){
            if(isset($a[$v['pid']])){
                $a[$v['pid']]['list'][]=&$a[$k];
            }else{
                $list[]=&$a[$k];
            }
        }
        unset($a);


        $s=$this->tre($list);

        echo "<table>";
        echo $s;
        echo "</table>";die;
        p($s);die;


        p($list);die;
    }
    public function auth(){
        $model=db('admin_role');
        if($_POST){
            $id=I("post.id",0,'intval');
            $ar=I("post.menu_id/a",[]);
            $id>0 || die(json_error('参数错误'));
            $str=base64_encode(serialize($ar));
            $res=$model->where(['id'=>$id])->update(['auth'=>$str,'update_time'=>time()]) || die(json_error('编辑失败'));
            die(json_success('编辑成功'));
        }

        $id=I("param.id",0,'intval');
        $info=$model->where(['id'=>$id])->find();
        $info || die(json_error('参数错误'));
        if($info['auth']=='all'){
            $auth_list='all';
        }else{
            $auth_list=unserialize(base64_decode($info['auth']));
        }

        $tree=new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $menu_list=db('menu')->select();
        foreach($menu_list as $k=>$v) {
            $menu_list[$k]['level'] = $tree->get_level($v['id'],$menu_list);
            if($auth_list=='all'){
                $checked = "checked";
            }else{
                if(is_array($auth_list)){
                    $checked=(in_array($v['id'], $auth_list))? ' checked' : '';
                }
            }
            if($v['pid']>0){
                $menu_list[$k]['checked'] = "<input type='checkbox' name='menu_id[]' value='".$v['id']."' class='J_checkitem' level='".$menu_list[$k]['level']."' ".$checked.">";
            }else{
                $menu_list[$k]['checked'] = '';
            }
            $menu_list[$k]['parentid_node'] = ($v['pid'])? ' class="child-of-node-'.$v['pid'].'"' : '';
        }
        $str  = "<tr id='node-\$id' \$parentid_node>" .
            "<td style='padding-left:10px;'>\$spacer \$checked \$name</td>
	                    </tr>";
        $tree->init($menu_list);
        $data=array();

        $data=$tree->get_tree(0, $str);  
        $this->assign('data',$data);
        $this->assign('info',$info);
        return $this->fetch('auth');
    }

    public function ajaxDelete(){
        $id=I("get.id",0,'intval');
        $menu=db('admin')->where(['role_id'=>$id])->delete();
        $res=db('admin_role')->where(['id'=>$id])->delete();
        $res || die(json_error('删除失败'));
        die(json_success('成功'));
    }
}